import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import Dao.AuthServerDao;
import security.Crypto;
import user.TicketForTgs;

public class AuthenticationServer implements Runnable {
	static ServerSocket serverSocket = null;
	static Socket socket = null;
	static DataInputStream dataInputStream = null;
	static DataOutputStream dataOutputStream = null;
	static int PORT_NO = 8888;
	static String SEED_AS_CLIENT = null;
	static String SEED = "ajay";

	AuthServerDao databseConnection = null;

	public void server() {
		databseConnection = new AuthServerDao();
		Thread thread = new Thread(this);
		thread.start();
	}

	public static void main(String[] args) {
		try {
			serverSocket = new ServerSocket(PORT_NO);
			System.out.println("Listening :" + PORT_NO);
			AuthenticationServer ls = new AuthenticationServer();
			ls.server();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void run() {
		System.out.println("Started The Authentication Server, Wating for User Request!!");
		System.out.println("------------------------------------------------------------");
		while (true) {
			try {
				socket = serverSocket.accept();
				dataInputStream = new DataInputStream(socket.getInputStream());
				dataOutputStream = new DataOutputStream(
						socket.getOutputStream());
				System.out.println("ip: " + socket.getInetAddress());

				String received = dataInputStream.readUTF();
				if (!received.equals(null)) {
					String[] message = received.split(" : ");
					String name = message[0];
					//System.out.println("The name is " + name);
					SEED_AS_CLIENT = databseConnection.getSeed(name);
					System.out.println("the seed is " + SEED_AS_CLIENT);
					String[] decrypt = Crypto.decrypt(SEED_AS_CLIENT,
							message[1]).split(" : ");

					System.out.println("Received Ticket request from "
							+ decrypt[0]);
					Boolean found = databseConnection.findUser(decrypt);
					if (found) {
						System.out.println("User verified With the database!!");
						TicketForTgs tgs = new TicketForTgs(SEED, 
								decrypt[0],
								1,
								Integer.parseInt(decrypt[1]),
								Integer.parseInt(decrypt[2]),
								Integer.parseInt(decrypt[3]),
								Integer.parseInt(decrypt[4]), decrypt[5],
								decrypt[6], 90000);
						/*System.out.println("The role is " + decrypt[1]);
						System.out.println("the period of time is "
								+ decrypt[2]);
						System.out.println("the hour is " + decrypt[3]);
						System.out.println("the day is " + decrypt[4]);
						System.out.println("the location is " + decrypt[5]);
						System.out.println("the physical address is "
								+ decrypt[6]);
						System.out.println(" The lifetime is" + 1800000000);
						*/
						String encryted = tgs.toClient(SEED_AS_CLIENT, SEED);
						System.out.println("Sending the ticket for TGS to "
								+ decrypt[0]);
						dataOutputStream.writeUTF(encryted);
						dataOutputStream.flush();
					} else {
						System.out
								.println("Invalid user data!! User not found");
						dataOutputStream.writeUTF("denied");
					}
					System.out.println("------------------------------------------------------------");
				}
				// System.out.println("message: " + dataInputStream.readUTF());
			} catch (Exception e) {
				//e.printStackTrace();
			} finally {
				try {
					if (socket != null) {
						socket.close();
					}
					if (dataInputStream != null) {
						dataInputStream.close();
					}
					if (dataOutputStream != null) {
						dataOutputStream.close();
					}
				} catch (IOException e) {
					//e.printStackTrace();
				}
			}

		}
	}

}